package com.bestv.xml.processor;

import com.bestv.utils.config.GlobalSettings;
import com.bestv.utils.file.FileFilterByPattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileFilter;

/**
 * Created by Henry on 14-7-10.
 */
public abstract class Processor {
    private Logger logger = LoggerFactory.getLogger(getClass());

    protected String xmlFilePattern;
    private File outputFolder;
    private File doneFolder;
    private File errorFolder;

    protected Processor() {
        String outputDirectory = GlobalSettings.getInstance().getOutputDirectory();
        outputFolder = new File(outputDirectory);
        if (!outputFolder.exists()) outputFolder.mkdirs();

        doneFolder = new File(outputFolder, "done");
        if (!doneFolder.exists()) doneFolder.mkdir();

        errorFolder = new File(outputFolder, "error");
        if (!errorFolder.exists()) errorFolder.mkdir();
    }

    protected File[] getFiles() {
        logger.info("Scan {} files from directory: {}", xmlFilePattern, outputFolder.getAbsolutePath());
        FileFilter filter = new FileFilterByPattern(xmlFilePattern);
        return outputFolder.listFiles(filter);
    }

    protected void moveToDoneFolder(File file) {
        file.renameTo(new File(doneFolder, file.getName()));
    }

    protected void moveToErrorFolder(File file) {
        file.renameTo(new File(errorFolder, file.getName()));
    }
}
